package io.renren.wh.interfaces.controller;

import io.renren.common.utils.Constant;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.wh.interfaces.entity.InterfacesEntity;
import io.renren.wh.interfaces.service.InterfacesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@Api(value = "接口信息管理",tags = "接口信息管理")
@RestController
@RequestMapping("wh/ware/interfaces")
public class InterfacesController {
    @Autowired
    private InterfacesService interfacesService;

    /**
     * 列表
     */
    @ApiOperation(value = "获取接口信息列表", notes = "获取接口信息列表", httpMethod = "GET")
    @RequestMapping("/list")
    // @RequiresPermissions("wh:interface:list")
    public R list(@ApiParam(name = "page", value = "第几页", required = false)
                      @RequestParam(required = false) String page,
                  @ApiParam(name = "limit", value = "每页显示的条数", required = false)
                      @RequestParam(required = false) String limit,
                  @ApiParam(name = "interfacename", value = "接口名称", required = false)
                      @RequestParam(required = false)String interfacename,
                  @ApiParam(name = "interfacecode", value = "接口标识", required = false)
                      @RequestParam(required = false)Integer interfacecode){

        Map<String,Object> params = new HashMap<>();
        params.put("interfacename",interfacename);
        params.put("interfacecode",interfacecode);
        params.put(Constant.PAGE,page);
        params.put(Constant.LIMIT,limit);
        PageUtils pageUtils = interfacesService.queryPage(params);

        log.info("page:{}",pageUtils);
        return R.ok().put("page", pageUtils);
    }


    /**
     * 信息
     */
    @ApiOperation(value = "获取接口信息", notes = "获取接口信息", httpMethod = "GET")
    @RequestMapping("/info/{id}")
    // @RequiresPermissions("wh:interfaces:info")
    public R info(
            @ApiParam(name = "id", value = "接口信息ID")
            @PathVariable("id") Integer id){
		InterfacesEntity interfaces = interfacesService.getById(id);

        return R.ok().put("interfaces", interfaces);
    }

    /**
     * 保存
     */
    @ApiOperation(value = "新增接口信息", notes = "新增接口信息", httpMethod = "POST")
    @RequestMapping("/save")
    @RequiresPermissions("wh:interfaces:save")
    public R save(@RequestBody InterfacesEntity interfaces){
		interfacesService.save(interfaces);

        return R.ok();
    }

    /**
     * 修改
     */
    @ApiOperation(value = "修改接口信息", notes = "修改接口信息", httpMethod = "POST")
    @RequestMapping("/update")
    @RequiresPermissions("wh:interfaces:update")
    public R update(@RequestBody InterfacesEntity interfaces){
		interfacesService.updateById(interfaces);

        return R.ok();
    }

    /**
     * 删除
     */
    @ApiOperation(value = "删除接口信息", notes = "删除接口信息", httpMethod = "DELETE")
    @DeleteMapping("/delete")
    @RequiresPermissions("wh:interfaces:delete")
    public R delete(
            @ApiParam(name = "ids", value = "接口信息ID")
            @RequestBody Integer[] ids){
		interfacesService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
